﻿using System;
using System.Linq;
using System.Web.Security;
using BusinessLogic.Interfaces;
using Domain.Entities;
using Ninject;


namespace BisinessLogic
{
    public class CustomRoleProvider : RoleProvider
    {
        [Inject]
        public IUsersRepository UsersRepository { get; set; }

        [Inject]
        public IRoleRepository RoleRepository { get; set; }


        public override bool IsUserInRole(string username, string roleName)
        {
            var user = UsersRepository.GetUserByLogin(username);
            var role = RoleRepository.GetRoleByName(roleName);
            if (user == null || role == null) return false;

            return user.RoleId == role.Id;
        }

        public override string[] GetRolesForUser(string username)
        {
            User user = UsersRepository.GetUserByLogin(username);
            Role role = RoleRepository.GetRoleById(user.RoleId);
            string[] mass = {role.Name};
            return mass;
        }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            return RoleRepository.GetRoles().Select(r => r.Name).ToArray();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName { get; set; }
    }
}